package com.easylinkin.linkappapi.grid.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.core.models.Operator;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.grid.models.dto.GridFileSaveDTO;
import com.easylinkin.linkappapi.grid.models.dto.GridHiddenDangerInfoDTO;
import com.easylinkin.linkappapi.grid.models.vo.GridHiddenDangerInfoVO;
import com.easylinkin.linkappapi.grid.service.IGridFileService;
import com.easylinkin.linkappapi.grid.service.IGridHiddenDangerInfoService;
import com.easylinkin.linkappapi.safe.dao.HiddenDangerInfoMapper;
import com.easylinkin.linkappapi.safe.entity.HiddenDangerInfo;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author kyj
 * @date 2024/5/31 17:40
 */
@Slf4j
@Setter(onMethod_ = @Resource)
@Validated
@Service
public class GridHiddenDangerInfoService extends ServiceImpl<HiddenDangerInfoMapper, HiddenDangerInfo> implements IGridHiddenDangerInfoService {

    private GridHiddenDangerService gridHiddenDangerService;
    private IGridFileService gridFileService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void close(GridHiddenDangerInfoDTO dto) {
        // 校验参数
        validParamRequired(dto);

        Operator operator = dto.getOperator();
        GridFileSaveDTO rectifyVoice = dto.getRectifyVoice();
        Date now = DateUtil.date();

        GridHiddenDangerInfoVO hiddenDanger = gridHiddenDangerService.getOneById(dto.getCheckAccountId());

        if (hiddenDanger == null || hiddenDanger.getStatus() == 2) {
            throw new BusinessException("操作失败,记录异常");
        }

        if (!operator.getUserId().equals(String.valueOf(hiddenDanger.getRectifyUid()))) {
            throw new BusinessException("您不是当前记录整改人!");
        }

        dto.setIsEnd(1);
        dto.setRectifyTime(now);
        dto.setCheckTime(now);

        if (dto.getCheckResult() == 1) {
            // 复查通过
            hiddenDanger.setStatus(2);// 已合格
            hiddenDanger.setCloseTime(new Date());
        }

        boolean dbFlag = this.save(dto);

        if (ObjectUtil.isNotNull(rectifyVoice)) {
            rectifyVoice.setBusId(dto.getId());
            gridFileService.saveDate(rectifyVoice);
        }

        // 修改隐患记录状态
        dbFlag = gridHiddenDangerService.updateById(hiddenDanger);
    }

    private void validParamRequired(HiddenDangerInfo appHiddenDangerInfo) {
        Assert.notNull(appHiddenDangerInfo, "参数为空");
        Assert.notNull(appHiddenDangerInfo.getStatus(), "隐患状态");
        Assert.notNull(appHiddenDangerInfo.getCheckAccountId(), "当前隐患记录id");

        if (appHiddenDangerInfo.getStatus() == 0) {
            // 待整改状态
            Assert.notNull(appHiddenDangerInfo.getRectifyResult(), "整改结果");
            Assert.notNull(appHiddenDangerInfo.getRectifyRemark(), "整改回复");
            Assert.isTrue(appHiddenDangerInfo.getRectifyRemark().length() <= 140, "回复字数过长");

        } else if (appHiddenDangerInfo.getStatus() == 1) {
            // 待复查状态
            Assert.notNull(appHiddenDangerInfo.getId(), "整改记录id");
            Assert.notNull(appHiddenDangerInfo.getCheckResult(), "复查结果");
            Assert.notNull(appHiddenDangerInfo.getCheckRemark(), "复查回复");
            Assert.isTrue(appHiddenDangerInfo.getCheckRemark().length() <= 140, "回复字数过长");
        }
        // Assert.isTrue(StringUtils.isNotBlank(appHiddenDangerInfo.getName()), "名称为空");
    }
}
